import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;
/**
 * Write a description of class box here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class box  extends object
{
    Zony z;
    int velY=0,a,y;
    boolean pushed=false,disp=false;
    
    protected void addedToWorld(World world)
    {
        z = (Zony) getWorld().getObjects(Zony.class).get(0);
    }  
    
    
    
    public void act() 
    {
        pushed=false;
        
        if(Math.abs(z.getX()-getX())<=13 && z.getY()<getY()+25 && z.getY()>getY()-14){
            if(z.getX()>getX())a=-1;else a=1;
            push(a);        
        }
        y=getY();
        if(ok(getX()+7,getY()+8) && ok(getX()-7,getY()+8) && velY!=14)
            velY++;
        
        setLocation(getX(),getY()+(int)(velY/2.0)); 
        
        while(!ok(getX()+7,getY()+7) || !ok(getX()-7,getY()+7) || object(getX(),getY()+1)){
            pushUp();
            velY=0;
        }
          
        
    }    
    
    public boolean stand(){
        return true;
    }
    
    public boolean ok(int a,int b){
        return ((it)getWorld()).ok(a,b);
    }
    
    public boolean push(int a){  
        if(pushed)return false;
        pushed=true;
        setLocation(getX()+2*a,getY());
        box b = (box) getOneIntersectingObject(box.class);
        setLocation(getX(),getY()-1);
        box c = (box) getOneIntersectingObject(box.class);
        setLocation(getX(),getY()+1);
        object box = (object) getOneIntersectingObject(object.class);
        if(!ok(getX()+8*a,getY()-5) || !ok(getX()+8*a,getY()-7)){
            setLocation(getX()-2*a,getY());
            if(disp)System.out.println(1);
            return false;
        }
        if(b!=null && !b.push(a)){
            setLocation(getX()-2*a,getY());
            if(disp)System.out.println(2);
            return false;
        }
        if(box!=null && b==null){
            setLocation(getX()-2*a,getY());
            if(disp)System.out.println(3);
            return false;
        }
        b = (box) getOneIntersectingObject(box.class);
        setLocation(getX(),getY()-3);
        c = (box) getOneIntersectingObject(box.class);
        setLocation(getX(),getY()+3);
        if(b==null && c!=null){
            setLocation(getX(),getY()-15);
            List arr = getIntersectingObjects(box.class);
            setLocation(getX(),getY()+15);
            for(int f=0;f<arr.size();f++){
                box d = (box) arr.get(f);
                d.push(a);
            }
            
        }
            
        
        
        
        return true;
   }
   
   public boolean object(int a,int b){
       object box = (object) getOneIntersectingObject(object.class);
       return box!=null;
    }
    
    public void pushUp(){
        if(disp)System.out.println(4);
        setLocation(getX(),getY()-1);
        box b = (box) getOneIntersectingObject(box.class);
        setLocation(getX(),getY()+1);
        box c = (box) getOneIntersectingObject(box.class);
        setLocation(getX(),getY()-1);
        if(b!=null && c==null){
            List d = getIntersectingObjects(box.class);
            for(int f=0;f<d.size();f++){
                box box = (box) d.get(f);
                box.pushUp();
            }
        }
    }
   
 
}
